<?php

include_once $_SERVER['DOCUMENT_ROOT'].'/nabLib/dbLib.php';

$id     = $_GET['id'];
$data    = $_GET['data'];
$pulse  = $_GET['pulse'];
$ping   = $_GET['ping'];

    $s_day    = $_GET['day'];
    if($s_day < 10){
        $s_day = sprintf("%02s", $s_day );
    } 
    $s_month  = $_GET['month'];
    if($s_month < 10){
        $s_month = sprintf("%02s", $s_month );
    } 
    $s_year   = $_GET['year'];
    $s_sec    = $_GET['sec'];
    if($s_sec < 10){
        $s_sec = sprintf("%02s", $s_sec );
    } 
    $s_min    = $_GET['min'];
    if($s_min < 10){
        $s_min = sprintf("%02s", $s_min );
    } 
    $s_hour   = $_GET['hour'];
    if($s_hour < 10){
        $s_hour = sprintf("%02s", $s_hour );
    }

$validated = dbValidateClientId($id);

if($validated == 0) {echo "no access";exit(0);}

if($ping == 1)
{
    echo("Ok");
}
elseif ($id > 0) 
{    
    //Explode
    $data_arr = explode(",",$data);
    
    //Get size of array;
    $length_data = count($data_arr);

    //echo "length:".$length_data." Pulse".$pulse."<br>";
    if($length_data != $pulse){echo "nabError pulse != length";exit(0);}
    
    
    //reverse array
    $data_rev = array_reverse($data_arr);
    
    //time array
    $data_time = array();
    
    $data_time[0] = 0; 
    for ($i = 1; $i<$length_data ; $i++ ) {  
        $data_time[$i] = $data_time[$i-1]+3600/$data_rev[$i];     
    }
    
    //Populate database
    for($i = 0; $i < $length_data ; $i++ ){
        
        //Translate delay to correct date and time
        $resDateTime_arr = subSecondsFromDate($data_time[$length_data-$i-1],$s_year,$s_month,$s_day,$s_hour,$s_min,$s_sec);
        $res = dbWriteSample($id,$resDateTime_arr['year'],$resDateTime_arr['month'],$resDateTime_arr['day'],$resDateTime_arr['hour'],$resDateTime_arr['min'],$resDateTime_arr['sec'],$data_arr[$i],1);  
        
    }
    if($res == 1)echo("Ok");
    
}
else
{
    echo("Error! Contact admin");
}
?>
